﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Model;
using static Model.AdminInfo;

namespace TakeoutMangesDAL
{
   public class AdminInfoDAL
    {
        public static Admininfo Login(string AdminUserName, string AdminUserPWD)
        {
            string sqltr = "select * from Admininfo where AdminUserName=@AdminUserName and AdminUserPWD=@AdminUserPWD";
            SqlParameter[] paras = new SqlParameter[] {
              new SqlParameter("@AdminUserName",AdminUserName),
                new SqlParameter("@AdminUserPWD",AdminUserPWD)
            };
            SqlDataReader reader = DBHelper.ExecuteReader(sqltr, CommandType.Text, paras);
            if (reader.Read())
            {
                Admininfo info = new Admininfo
                {
                    AdminUserId = Convert.ToInt32(reader["AdminUserId"]),
                    AdminUserName = reader["AdminUserName"].ToString(),
                    AdminUserPWD = reader["AdminUserPWD"].ToString(),
                };
                return info;
            }
            return null;
        }
        public static List<Admininfo> AdminInfos()
        {
            string sqltr = "select * from Admininfo";
            SqlDataReader reader = DBHelper.ExecuteReader(sqltr, CommandType.Text, null);
            List<Admininfo> list = new List<Admininfo>();
            while (reader.Read())
            {
                Admininfo info = new Admininfo
                {
                    AdminUserId = Convert.ToInt32(reader["AdminUserId"]),
                    AdminUserName = reader["AdminUserName"].ToString(),
                    AdminUserPWD = reader["AdminUserPWD"].ToString(),
                };
                list.Add(info);
            }
            reader.Close();
            return list;
        }
        public static int DeleteAdmin(int AdminUserId)
        {
            string sqltr = "delete from Admininfo where AdminUserId=@AdminUserId";
            SqlParameter[] paras = new SqlParameter[] {
            new SqlParameter("@AdminUserId",AdminUserId),
            };
            return DBHelper.ExecuteNonQuery(sqltr, paras);
        }
        public static int InsertAdmin(Admininfo info)
        {
            string sqltr = "insert into Admininfo values(@AdminUserName,@AdminUserPWD,@Isdelete)";
            SqlParameter[] paras = new SqlParameter[] {
            new SqlParameter("@AdminUserName",info.AdminUserName),
            new SqlParameter("@AdminUserPWD",info.AdminUserPWD),
            new SqlParameter("@Isdelete",info.Isdelete)
            };
            return DBHelper.ExecuteNonQuery(sqltr, paras);
        }
    }
}
